/**
 * 证券操作类的插件。
 * 依赖：
 * 1、http://lib.sinaapp.com/js/jquery/1.8.3/jquery.min.js
 * 2、jquery-ui-1.9.2.custom.min.js
 */
(function($){
    /**
     * 系统全局命名空间。
     */
    $.yilab = $.yilab || {};
    $.yilab.Cache = $.yilab.Cache || { "Stocks" : {} };
    var Cache = $.yilab.Cache["Stocks"];
    Cache["_stocks"] = [];
    
    /**
     * 证券自动完成组件。
     * @param {String} selector 页面元素的选择器。
     */
    function StockAutoComplete(selector){
        this.selector = selector;
        this.service = "/svr/getStocks.php";
        this.initialize();
    }
    StockAutoComplete.prototype = {
        /**
         * 初始化。
         */
        initialize : function(){
            $(this.selector).autocomplete({ 
                    minLength : 1,
                    source : $.proxy(this.search, this) 
                });
            if(0 == Cache["_stocks"].length){
                $.post(this.service, {"key" : ""}, function(data){
                        var json = [];
                        try{
                            json = $.parseJSON(data);
                            if(json){
                                Cache["_stocks"] = json;
                            }
                        }catch(ex){
                        }
                });
            }
        },
        
        search : function(request, response){
            var key = request.term,
                firstChar = key[0],
                stocks = [],
                res = response;
            if(Cache.hasOwnProperty(key)){
                stocks = Cache[key];
                response(stocks);
            }else if(Cache.hasOwnProperty(firstChar)){
                stocks = this.match(key, Cache[firstChar]);
                Cache[key] = stocks;
                response(stocks);
            }else if(Cache["_stocks"].length > 0){
                stocks = this.match(key, Cache["_stocks"]);
                Cache[key] = stocks;
                response(stocks);
            }else{
                $.post(this.service, {"key" : key}, function(data){
                        var json = [];
                        try{
                            json = $.parseJSON(data);
                            if(json){
                                Cache[key] = json;
                                response(json);
                            }
                        }catch(ex){
                        }
                });
            }
        },
        
        match : function(key, stocks){
            var result = [],
                reg = new RegExp(key, "ig");
            for(var i = 0; i < stocks.length; i++){
                var stock = stocks[i];
                if(reg.test(stock["label"]) || 
                    reg.test(stock["value"]) || 
                    reg.test(stock["pinyin"])){
                    result.push(stock);
                }
            }
            return result;
        }
    }
    
    /**
     * 页面操作类的命名空间。
     */
    $.yilab.stocks = {
        /**
         * 证券自动完成组件。
         * @param {String} selector 页面元素的选择器。
         */
        stockAutoComplete : function(selector){
            return new StockAutoComplete(selector);
        }
    };    
})(jQuery);
